package com.huluobo.document.view

import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.document.DocIntent
import com.huluobo.document.DocViewModel
import com.huluobo.document.entity.DocEntity
import com.huluobo.document.entity.DocEntityItem
import com.huluobo.lib_base.BaseTitle
import com.huluobo.lib_base.HttpType
import com.huluobo.lib_base.RoutePath
import com.huluobo.lib_base.UIState
import kotlinx.coroutines.launch

/**
 *  Created by LC on 2025/1/15.
 */

@Composable
fun DocPage(navHostController: NavHostController, vm: DocViewModel = hiltViewModel()) {
    val list = remember {
        SnapshotStateList<DocEntityItem>()
    }

    var index by remember {
        mutableStateOf(-1)
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UIState.onError -> {
                        Log.e("TagA", "error:${it.msg}")
                    }

                    UIState.onLoading -> {}
                    is UIState.onSuccess<*> -> {
                        if (it.type == HttpType.DELETE) {
                            ToastUtils.showLong("删除成功")
                            list.removeAt(index)
                        } else {
                            list.clear()
                            list.addAll(it.data as DocEntity)
                        }
                    }
                }
            }
        }
        vm.sendIntent(DocIntent.getDoc)
    })

    Scaffold(floatingActionButton = {
        FloatingActionButton(onClick = {
            //跳转到本地文件页面
            navHostController.navigate(RoutePath.LOCAL)
        }) {
            Icon(imageVector = Icons.Default.Add, contentDescription = "")
        }
    }) {
        Column(
            Modifier
                .padding(it)
                .padding(10.dp)
        ) {
            BaseTitle(title = "文件", navHostController = navHostController)
            Spacer(modifier = Modifier.height(10.dp))
            LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp), content = {
                items(list) {
                    index = list.indexOf(it)
                    DocItem(entity = it, vm) {
                        index = list.indexOf(it)
                    }
                }
            })
        }
    }
}

@Composable
fun DocItem(entity: DocEntityItem, vm: DocViewModel, onClick: () -> Unit) {
    var isShow by remember {
        mutableStateOf(false)
    }

    Row(Modifier.clickable {
        isShow = true
    }) {
        AsyncImage(
            model = entity.url,
            contentDescription = "",
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .size(50.dp)
                .clip(
                    RoundedCornerShape(20.dp)
                )
        )
        Spacer(modifier = Modifier.width(10.dp))
        Column(Modifier.height(50.dp), verticalArrangement = Arrangement.SpaceBetween) {
            Text(text = entity.objName)
            Text(text = entity.createTime)
        }
    }

    if (isShow) {
        AlertDialog(onDismissRequest = { isShow = false },
            confirmButton = {
                Text(text = "确认", Modifier.clickable {
                    vm.sendIntent(DocIntent.deleteDoc(entity.id))
                    onClick()
                    isShow = false
                })
            },
            title = { Text(text = "删除提醒") },
            text = { Text(text = "确认删除${entity.objName}吗?") }
        )
    }
}